<?php

Yii::import('gii.generators.crud.CrudCode');

class Crud80Code extends CrudCode {

	public function generateActiveLabel($modelClass, $column) {
		$tab = $this->_genTab(2);
		return "{$tab}echo \$form->labelEx(\$model, '{$column->name}');";
	}

	public function generateActiveField($modelClass, $column) {
		$tab = $this->_genTab(2);
		if ($column->type === 'boolean')
			return "{$tab}techo \$form->checkBox(\$model, '{$column->name}');";
		else if (stripos($column->dbType, 'text') !== false)
			return "{$tab}\$htmlOptions = array('rows'=>6, 'cols'=>50);\n{$tab}echo \$form->textArea(\$model, '{$column->name}', \$htmlOptions);";
		else if (stripos($column->dbType, 'date') !== false) {
			$tab3 = $this->_genTab(3);
			$tab4 = $this->_genTab(4);
			$l = array(
				"$tab\$properties = array(\n",
				"$tab3'model'=>\$model,\n",
				"$tab3'attribute'=>'{$column->name}',\n",
				"$tab3'options'=>array(\n",
				"$tab4'dateFormat'=>'yy-mm-dd',\n",
				"$tab4'changeYear'=>TRUE,\n",
				"$tab4'changeMonth'=>TRUE,\n",
				"$tab3),\n",
				"$tab);\n",
				"echo \$this->widget('zii.widgets.jui.CJuiDatePicker', \$properties);"
			);
			return implode('', $l);
		}
		else {
			if (preg_match('/^(password|pass|passwd|passcode)$/i', $column->name))
				$inputField = 'passwordField';
			else
				$inputField = 'textField';

			if ($column->type !== 'string' || $column->size === null)
				return "{$tab}echo \$form->{$inputField}(\$model, '{$column->name}');";
			else {
				if (($size = $maxLength = $column->size) > 60) {
					$size = 60;
				}
				return "{$tab}\$htmlOptions = array('size'=>$size, 'maxlength'=>$maxLength);\n{$tab}echo \$form->{$inputField}(\$model, '{$column->name}', \$htmlOptions);";
			}
		}
	}
	
	protected function _genTab($count) {
		$tab = '';
		foreach (range(1, $count) as $i) {
			$tab .= "\t";
		}
		return $tab;
	}

}
